package com.example.student_manager_backend.controller;


import com.example.student_manager_backend.entity.Course;
import com.example.student_manager_backend.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @PostMapping
    public ResponseEntity<Course> createCourse(@RequestBody Course course) {
        Course createdCourse = courseService.createCourse(course);
        return ResponseEntity.ok(createdCourse);
    }

    @PutMapping("/{courseId}")
    public ResponseEntity<Course> updateCourse(@PathVariable String courseId, @RequestBody Course course) {
        Course updatedCourse = courseService.updateCourse(courseId, course);
        if (updatedCourse != null) {
            return ResponseEntity.ok(updatedCourse);
        }
        return ResponseEntity.notFound().build();
    }

    @DeleteMapping("/{courseId}")
    public ResponseEntity<Void> deleteCourse(@PathVariable String courseId) {
        courseService.deleteCourse(courseId);
        return ResponseEntity.ok().build();
    }

    @GetMapping
    public ResponseEntity<Page<Course>> findCourses(
            Specification<Course> spec,
            @PageableDefault(size = 10) Pageable pageable) {
        Page<Course> courses = courseService.findCourses(spec, pageable);
        return ResponseEntity.ok(courses);
    }

    @GetMapping("/{courseId}")
    public ResponseEntity<Course> findCourseById(@PathVariable String courseId) {
        return courseService.findCourseById(courseId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping("/{courseId}/enroll/{studentId}")
    public ResponseEntity<Optional<Course>> enrollStudent(
            @PathVariable String courseId,
            @PathVariable String studentId) {
        try {
            Optional<Course> course = courseService.enrollStudent(courseId, studentId);
            return ResponseEntity.ok(course);
        } catch (IllegalStateException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @PostMapping("/{courseId}/drop/{studentId}")
    public ResponseEntity<Optional<Course>> dropStudent(
            @PathVariable String courseId,
            @PathVariable String studentId) {
        try {
            Optional<Course> course = courseService.dropStudent(courseId, studentId);
            return ResponseEntity.ok(course);
        } catch (IllegalStateException e) {
            return ResponseEntity.badRequest().build();
        }
    }
} 